package com.bgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.mapper.SocialMapper;
import com.bgs.pojo.Detailed;
import com.bgs.pojo.ResumePicture;
import com.bgs.pojo.Social;
import com.bgs.service.DetailedService;
import com.bgs.service.SocialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.utils.LocaldatetimeUtils;
import com.bgs.vo.SocialVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 社保表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-08
 */
@Service
public class SocialServiceImpl extends ServiceImpl<SocialMapper, Social> implements SocialService {

    @Autowired
    private DetailedService detailedService;

    @Override
    public Social selectByPrimaryKey(Integer sid) {
        return baseMapper.selectByPrimaryKey(sid);
    }

    @Override
    public List<SocialVo> findAllSocial() {
        //调用service的方法实现查询所有的讲师
        List<Social> list = this.list(null);
        List<SocialVo> slist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SocialVo s = new SocialVo();
            BeanUtils.copyProperties(list.get(i),s);
            s.setTime(LocaldatetimeUtils.timess(list.get(i).getPayment()));

            QueryWrapper<Detailed> w1 = new QueryWrapper<>();
            w1.eq("sid",list.get(i).getSid());//关联社保表id
            w1.eq("type",1);//社保明细 1是社保明细
            List<Detailed> list1 = detailedService.list(w1);
            Double ltotalp = 0D;//个人金额缴纳合计
            Double ltotalg = 0D;//公司金额缴纳合计
            for (Detailed d : list1) {
                ltotalp += d.getPerson();
                ltotalg +=d.getCompany();
            }
            s.setShebao(ltotalp);

            QueryWrapper<Detailed> w2 = new QueryWrapper<>();
            w2.eq("sid",list.get(i).getSid());//关联社保表id
            w2.eq("type",2);//社保明细 2 是公积金明细
            List<Detailed> list2 = detailedService.list(w2);
            Double ltotalgs = 0D;//公司金额缴纳合计
            for (Detailed d : list2) {
                ltotalgs +=d.getCompany();
            }
            Detailed d2 = new Detailed();
            s.setGongjijin(ltotalgs);

            slist.add(s);
        }
        return slist;
    }

    @Override
    public ResumePicture selectByPIc(Integer eid) {
        return baseMapper.selectByPIc(eid);
    }


}
